package org.seckill.web;

import com.sun.org.apache.xpath.internal.operations.Mod;
import org.seckill.dto.Exposer;
import org.seckill.dto.SeckillExcution;
import org.seckill.dto.SeckillResult;
import org.seckill.entity.Seckill;
import org.seckill.enums.SeckillStatEnum;
import org.seckill.exception.RepeatKillException;
import org.seckill.exception.SeckillCloseException;
import org.seckill.service.SeckillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by chenhui479 on 16/11/20.
 */

@Controller
@RequestMapping("/seckill")
public class SeckillController {


    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private SeckillService seckillService;

    @RequestMapping(value="/list",method= RequestMethod.GET)
    public String list(Model model){


        List<Seckill> seckillList = seckillService.getSeckillList();
        model.addAttribute("list",seckillList);

        return "list";


    }


    @RequestMapping(value="/{seckillId}/detail",method = RequestMethod.GET)
    public  String detail( @PathVariable("seckillId") Long seckillId, Model model){

        if(seckillId == null){
            return "redirect:/seckill/list";
        }

        Seckill seckill = seckillService.getById(seckillId);
        if(seckill==null){
            return "redirect:/seckill/list";
        }

        model.addAttribute("seckill",seckill);
        return "detail";
    }


    @RequestMapping(value="/{seckillId}/exposer",method = RequestMethod.POST,
       produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SeckillResult<Exposer> exposer(@PathVariable("seckillId") Long seckillId){


        SeckillResult<Exposer> result;

        try {
            Exposer exposer = seckillService.exportSeckillUrl(seckillId);

            result = new SeckillResult<Exposer>(true,exposer);

        }catch (Exception e){

            logger.error(e.getMessage(),e);
            result = new SeckillResult<Exposer>(false,"error="+e.getMessage());

        }

        return  result;

    }


    @RequestMapping(value="/{seckillId}/{md5}/execution",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SeckillResult<SeckillExcution> excute(@PathVariable("seckillId") Long seckillId,@PathVariable("md5") String md5,
                                             @CookieValue(value="killPhone",required = false) Long phone){

        SeckillResult<SeckillExcution> result;

        if(phone == null){
            result = new SeckillResult<SeckillExcution>(false,"未注册");
        }

        try {
            SeckillExcution seckillExcution = seckillService.excuteSeckiillProcdure(seckillId,phone,md5);

            result = new SeckillResult<SeckillExcution>(true,seckillExcution);

        }catch (SeckillCloseException e1){
            SeckillExcution seckillExcution = new SeckillExcution(seckillId, SeckillStatEnum.END);
            result = new SeckillResult<SeckillExcution>(true,seckillExcution);

        }catch (RepeatKillException e2){
            SeckillExcution seckillExcution = new SeckillExcution(seckillId, SeckillStatEnum.REPEAT_KILL);
            result = new SeckillResult<SeckillExcution>(true,seckillExcution);

        }

        catch (Exception e){

            logger.error(e.getMessage(),e);
            SeckillExcution seckillExcution = new SeckillExcution(seckillId, SeckillStatEnum.INNER_ERROR);
            result = new SeckillResult<SeckillExcution>(true,seckillExcution);

        }

        return  result;
    }

    @RequestMapping(value="/time/now",method = RequestMethod.GET)
    @ResponseBody
    public SeckillResult<Long> time(){

        Date date = new Date();

        return new SeckillResult<Long>(true,date.getTime());



    }

}
